package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.clqteam.dao.updataZsDao;
import com.clqteam.model.ZsProduct;
import com.clqteam.util.JDBCutil;

public class updataZsDaoImpl implements updataZsDao{

	@Override
	public int updateZsByZspid(int zsId, int zspAmount) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE s_zs SET s_zs.zspAmount = ?");
		sql.append(" WHERE s_zs.zsId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setInt(1, zspAmount);
			pst.setInt(2, zsId);
			m = pst.executeUpdate();
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return m;
	}

	@Override
	public int updateShByZspid(int zspId, int zspAmount) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE s_product SET s_product.pAmount = ?");
		sql.append(" WHERE s_product.pId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, zspAmount);
			pst.setInt(2, zspId);
			m = pst.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return m;
	}


	@Override
	public int getOldZsInfoByZspid(int zsId) {
		int zp = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_zs");
		sql.append(" WHERE s_zs.zsId=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst= con.prepareStatement(sql.toString());
			pst.setInt(1,zsId );
			ResultSet rst= pst.executeQuery();
			while (rst.next()) {
				zp=rst.getInt("zspAmount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zp;
	}

	@Override
	public void setdirInfo(String drInfo, String drTime, int eId, String eName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO s_dailyreport");
		sql.append(" (drTime,drInfo,eId,eName) VALUES (?,?,?,?)");
		Connection con  = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setString(1, drTime);
			pst.setString(2, drInfo);
			pst.setInt(3, eId);
			pst.setString(4, eName);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public int getProductCountByZspid(int zspId) {
		int zp = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT s_product.pAmount FROM s_product");
		sql.append(" WHERE s_product.pId=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst= con.prepareStatement(sql.toString());
			pst.setInt(1,zspId );
			ResultSet rst= pst.executeQuery();
			while (rst.next()) {
				zp=rst.getInt("pAmount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zp;
	}
	
	@Override
	public int getOidByPid(int zspId) {
		int m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_orderinfo.oId FROM s_orderinfo");
		 sql.append(" WHERE s_orderinfo.pId=?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, zspId);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				m=rst.getInt("oId");
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return m;
	}

	@Override
	public void updataOrder(int oId, double oMoney) {
		 StringBuffer sql = new StringBuffer();
		 sql.append(" UPDATE s_order SET s_order.oMoney=?");
		 sql.append(" WHERE oId = ?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setDouble(1, oMoney);
			pst.setInt(2, oId);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void updataOrderInfo(int oId, int pAmount) {
		StringBuffer sql = new StringBuffer();
		 sql.append(" UPDATE s_orderinfo SET s_orderinfo.poAmount=?");
		 sql.append(" WHERE oId = ?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pAmount);
			pst.setInt(2, oId);
			
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public double getPcostfromZs(int zsId) {
         double m = 0;
		StringBuffer sql = new StringBuffer();
		 sql.append(" SELECT s_zs.zspCost FROM s_zs");
		 sql.append(" WHERE s_zs.zsId=?");
		 Connection con = JDBCutil.getConnection();
		 try {
			PreparedStatement pst =con.prepareStatement(sql.toString());
			pst.setInt(1, zsId);
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				m=rst.getDouble("zspCost");
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return m;
	}


}
